﻿<%@ page language="java" import="com.newcrm.bean.*,com.newcrm.config.*,com.newcrm.util.Util,java.util.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
  User user = (User) session.getAttribute("user");
  Map<Integer, CompanyInfo> companys = (Map<Integer, CompanyInfo>) application.getAttribute(Const.APPLICATION_COMPANYS);
  CompanyInfo companyInfo = companys.get(Integer.valueOf(user.companyId));
  List<Map<String, Object>> customerCategorys = companyInfo.customerCategorys;
  List<User> users = companyInfo.users;
  String phone = request.getParameter("phone");
  if (phone == null) {
    phone = "";
  }
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>添加客户 - 添加公司</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <meta name="renderer" content="webkit"/>
  <meta http-equiv="X-UA-Compatible" content="IE=Edge">
  <link rel="shortcut icon" href="<%=request.getContextPath()%>/images/qikebao.ico" />
  <link rel="stylesheet" href="<%=request.getContextPath()%>/css/all.css?ver=<%=Const.VERSION%>">
  <link rel="stylesheet" href="<%=request.getContextPath()%>/css/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="<%=request.getContextPath()%>/css/datepicker.css" />
  <link rel="stylesheet" href="<%=request.getContextPath()%>/css/bootstrap-datetimepicker.min.css" />
  <link rel="stylesheet" href="<%=request.getContextPath()%>/css/magnific-popup.css">
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/jquery-1.11.1.min.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/bootstrap.min.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/jquery-ui.min.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/jquery.bootstrap-growl.min.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/moment.min.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/bootstrap-datepicker.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/bootstrap-datetimepicker.min.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/daterangepicker.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/jquery.magnific-popup.min.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/jquery.cookie.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/popbox.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/visiblecommon.js?ver=<%=Const.VERSION%>"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/companySearch.js?ver=<%=Const.VERSION%>"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/commonCustomer_new.js?ver=<%=Const.VERSION%>"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/area.js?ver=<%=Const.VERSION%>"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/common.js?ver=<%=Const.VERSION%>"></script>
  <script>
    var customerCategorys = new Array();
    var _share_type_user = '<%=Const.SHARE_TYPE_USER%>';
    var _share_type_group = '<%=Const.SHARE_TYPE_GROUP%>';
    var _visible_person = '<%=Const.VISIBLE_PERSON%>';
    <%if (customerCategorys != null) {
        for (Map<String, Object> category:customerCategorys) {
          String color = (String) category.get("Color");
          if (color == null || color.trim().length() == 0) {
            color = "000000";
          }%>
    customerCategorys.push({id:"<%=category.get("CategoryId")%>",name:"<%=Util.UEncode((String)category.get("CategoryName"))%>", color:"<%=color%>"});
      <%}
    } %>
    var allUsers = new Array();
    <%if (users != null) {
        for (User u:users) { %>
    allUsers.push({id:"<%=u.userId%>",name:"<%=Util.UEncode(u.name)%>",photo:"<%=u.imgPath%>"});
    <%  }
      } %>
    $(function() {
      var isExistName = false;
      if ($("#customer_phone").val() != "") {
        $("#customer_phone_title").hide();
        $("#customer_phone_container").show();
      }

      $("#companyName").focus();

      $("#companyName").blur(function(){
        var params = {};
        params.companyName = $("#companyName").val();
        if (params.companyName == "") {
          $("#txt").show();
          $("#success").hide();
          $("#fail").hide();
          return;
        }
        isExistName = false;
        $.ajax({
          url: contextPath + '/company/checkCompanyName?random=' + Math.random(),
          type: 'GET',
          data:params,
          dataType: 'json',
          timeout: 60000,
          error: function(){
          },
          success: function(json){
            $("#txt").hide();
            if (json.result == "1") {
              $("#success").hide();
              $("#fail").show();
              isExistName = true;
            } else {
              $("#success").show();
              $("#fail").hide();
              isExistName = false;
            }
          }
        });
      });
      $("#add_link").click(function(e) {
        e.preventDefault();
        $('#newForm').submit();
      });

      $('#newForm').submit(function(event){
        if (isExistName) {
          alert("该公司名称已经存在，请选择其他的公司名称");
          $("#companyName").focus();
          return false;
        }
        if ($("#companyName").val() == "") {
          alert("请输入公司名称");
          $("#companyName").focus();
          return false;
        }
        var _tel = $("#phone_show input").map(function() {return $(this).val();
        }).get().join();
        if (_tel != "") {
          var _check = checkTelExist(_tel);
          if (_check != 1) {
<%  if (user.isAdmin || user.isGroupAdmin) {%>
            var r=confirm("您提交的电话号码与以下客户重复：\r" + _check + " \r 是否继续提交新客户？");
            if (!r) {
              return false;
            }
<% } else { %>
            alert("您提交的电话号码与以下客户重复，无法新创建。\r" + _check);
            return false;
<% }%>
          }
        }

        var _qq = $("#im_show input").map(function() {return $(this).val();
        }).get().join();
        if (_qq != "") {
          var _check = checkQQExist(_qq);
          if (_check != 1) {
<%  if (user.isAdmin || user.isGroupAdmin) {%>
            var r=confirm("您提交的QQ与以下客户重复：\r" + _check + " \r 是否继续提交新客户？");
            if (!r) {
              return false;
            }
<% } else { %>
            alert("您提交的QQ与以下客户重复，无法新创建。\r" + _check);
            return false;
<% }%>
          }
        }

        var emailinputError = false;
        $("input[name='email']").each(function (i) {
          if (!checkEmail($(this).val())) {
            emailinputError = true;
            alert("邮箱格式不正确");
            $(this).focus();
          }
        });
        if (emailinputError) {
          return false;
        }
        return true;
      });
    });
  </script>
</head>
<body class="parties">
  <jsp:include page="topNavi.jsp" flush="true"/>
  <div class="main-page">
    <table>
    <tr>
      <td class="main-body">
        <div class="topnavi">
          <div class='leftMenu'>
            <span>添加公司</span> <span class="s"> 或 <a href="<%=request.getContextPath()%>/people/new?phone=<%=phone%>">添加联系人</a></span>
          </div>
        </div>
        <div class="edit-page">
          <form action="<%=request.getContextPath()%>/company" autocomplete="off" method="post" id="newForm">
            <input type="hidden" name="operation" value="new">
            <div class="page_divider">
              <div class="primary_fields">
              <table cellpadding="0" cellspacing="0" class="contact_types">
              <tr class="name">
                <th><h2>公司名称</h2></th>
                <td><input class="name autofocus" id="companyName" name="companyName" size="30" type="text" /> <span id="txt" class="txt">(必填)</span><span id="success" class="success"><img src="<%=request.getContextPath()%>/images/icon_success.png"></span><span id="fail" class="fail"><img src="<%=request.getContextPath()%>/images/icon_fail.png">该公司名称已经存在，请选择其他的公司名称</span></td>
              </tr>
              <tr>
                <th><h2>客户分类</h2></th>
                <td class="selectbox">
                  <select name="categoryId"><option value="0">无</option>
                  <%
                  if (customerCategorys != null) {
                    for (Map<String, Object> category:customerCategorys) { %>
                    <option value="<%=category.get("CategoryId")%>"><%=Util.HTMLEncode((String)category.get("CategoryName"))%></option>
                  <%
                    }
                  }
                  %>
                  </select><% if (user.isAdmin) {%> <a href="<%=request.getContextPath()%>/customercategory?categoryType=<%=Const.CUSTOMER_CATEGORY_COMPANY%>">设置客户分类</a><%}%>
                </td>
              </tr>
              </table>
              </div>
            </div>
            <jsp:include page="commonCustomer_new.jsp" flush="true"/>
            <p class="submit">
              <a id="add_link" class="button blue" href="#"><i class="fa fa-check"></i> 添加公司</a>
              <a class="button grey" href="<%=request.getContextPath()%>/parties"><i class="fa fa-times"></i> 取消</a>
            </p>
          </form>
        </div>
      </td>
      <td class="main-sidebar">
        <div class="content">
          <h2 class="clearfix">可以方便地导入/导出客户信息</h2>
          <ul>
            <li><a href="<%=request.getContextPath()%>/customerimports/new"><i class="fa fa-file-excel-o"></i> 导入Excel/CSV文件</a></li>
          </ul>
          <h2>撞单查询</h2>
          <div class="top5"><a href="#search-popup" class="open-popup-link button small grey"><i class="fa fa-search"></i> 撞单查询</a></div>
        </div>
      </td>
    </tr>
    </table>
  </div>

  <div id="search-popup" class="white-popup big mfp-hide">
    <div class="popup-title"><span class="title">撞单查询</span> <span class="popup-close-button"><i class="fa fa-times"></i>&nbsp;</span></div>
    <div class="popup-content">
      <div class="advsearch">
        <p class="title">检索系统中的客户信息，避免撞单</p>
        <form id="search_customers" method="post" >
          <table class="searchDetail">
            <tr>
              <td>名称</td><td><input class="autofocus" type="text" value="" id="txtCompanyName"></td>
              <td>电话</td><td><input class="autofocus" type="text" value="" id="txtTel"></td>
            </tr>
            <tr>
              <td>邮箱</td><td><input class="autofocus" type="text" value="" id="txtEmail"></td>
              <td>QQ</td><td><input class="autofocus" type="text" value="" id="txtQQ">
              <span id="span_submit"><a id="search-link" class="button blue small" href="#"><i class="fa fa-search"></i> 搜索</a> <span id="noresult_msg">没检索到符合条件的结果</span></span>
              </td>
            </tr>
          </table>
        </form>
        <table class="searchresult" id="contactsList"></table>
      </div>
    </div>
  </div>
</body>
</html>